package com.jade.common.utils;/*package com.jade.common.utils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Component
public class Send2ServerUtil {
	private static final Logger log = LoggerFactory.getLogger(Send2ServerUtil.class);
	@Autowired
	private RestTemplate restTemplate;
	
	//true 用http发送，其他用内部服务调用
	@Value("${demo.postSwitch}")
	public String postSwitch;

	//true 用http发送，其他用内部服务调用
	@Value("${demo.urlPath}")
	public String urlPath;
	
	@Autowired
    private ApplicationContext applicationContext;
	
	
	*//**
	 * jsonParam是一个Map集合的参数，分两部分，
	 * 1、类名或者是路径  key是serverName   value是值ps map.put("methodUrl","demoService.demoServiceTest")
	 * @param jsonParam
	 * @return
	 *//*
	@SuppressWarnings("unchecked")
	public CommonResponse send2Server(Map <String,Object> paramMap) {
		try {
			log.info("调用服务请求"+paramMap.get("methodUrl")+"");
		} catch (Exception e2) {
			e2.printStackTrace();
			return CommonResponse.failResponse("方法名不存在");
		}
		
		//用集群服务。就用restTemplate，单点部署，就从AppletContext里面取bean
		if(StringUtils.equals("true", postSwitch)){
			
			return sendHttp(paramMap);
			
		}else{
			
			return localCall(paramMap);
			
		}
	}


	*//**
	 * 本地调用，返回最后调用结果，第一个参数是转成的Object对象，第二个参数是传过来的对象
	 * @param parseObject
	 * @param jsonParam
	 * @return
	 *//*
	private CommonResponse localCall(Map<String, Object> jsonParam) {
		String invoke="";
		String[] split;
		Method[] methods;
		Object bean;
		String serverName="";
		try {
			serverName=jsonParam.get("methodUrl")+"";
			log.info("调用本地方法:"+serverName);
			split = serverName.split("\\.");
			Class<?> Classes = applicationContext.getBean(split[1]).getClass();
			methods = Classes.getMethods();
			bean = applicationContext.getBean(Classes);
		} catch (Exception e1) {
			e1.printStackTrace();
			log.error("调用本地方法失败：url="+serverName);
			return CommonResponse.failResponse("方法名调用错误");
		} 
		for (Method method : methods) {
			try {
				String name = method.getName();
				System.out.println("当前方法名为"+name+"目标方法名为"+split[2]);
				if(StringUtils.equals(split[2], name)){
						 invoke = (String) method.invoke(bean, jsonParam);
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("调用本地方法失败：url="+serverName);
				return CommonResponse.failResponse(e.getMessage());
			} 
		}
		return CommonResponse.successResponse(invoke) ;
	}


	*//**
	 * HTTP调用，第一个参数是地址参数，第二个是入参。
	 * @param split
	 * @param parseObject
	 * @return
	 *//*
	private CommonResponse sendHttp(Map<String, Object> jsonParam) {
		String serverName= jsonParam.get("methodUrl")+"";
		String[] split = serverName.split("\\.");
		if(!(split.length==3)){
			return CommonResponse.failResponse("方法名格式不合法");
		}
		if(StringUtils.equals("post", jsonParam.get("requestMethod")+"")){
			Map<String,Object> body = restTemplate.postForEntity(urlPath+"/"+split[0]+"/"+split[1]+"/"+split[2], jsonParam.get("paramMap"), HashMap.class).getBody();
			log.info(body.toString());
			return CommonResponse.successResponse(body);
		}else{
			Map<String,Object> paramMap = (Map<String, Object>) jsonParam.get("paramMap");
			Set<String> keySet = paramMap.keySet();
			String url=urlPath+"/"+split[0]+"/"+split[1]+"/"+split[2]+"?";
			for (String string : keySet) {
				url=url+"{"+string+"}"+"&";
			}
			url=url.substring(0,url.length()-1);
			log.info("调用服务："+url);
			String res = restTemplate.getForEntity(urlPath+"/"+split[0]+"/"+split[1]+"/"+split[2],String.class,paramMap).getBody();
			log.info("调用服务成功返回值为："+res);
			return CommonResponse.successResponse("成功",res);
		}
	}

}
*/